﻿using H2.NET.Common;
using H2.NET.Data.Common;
using H2.NET.Web.Pages.SMD.Model;
using H2.NET.Web.Pages.SMD.Service;
using H2.NET.Web.Workflow.DwzCode;
using System;
using System.Collections.Generic;
using H2.NET.Web.Workflow.Helper;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using H2.NET.Web.Pages.SMD.Helper;
using Ultimus.WFServer;

namespace H2.NET.Web.Pages.SMD
{
    public partial class FinanceBatchConfirm : BasePage
    {
        public List<t_StoreMasterData> data = new List<t_StoreMasterData>();
        public Dictionary<int, List<string>> info = new Dictionary<int, List<string>>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var qIncidents = AksRequest.GetString("incidents");
                List<string> incidents = qIncidents.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                data = (t_StoreMasterData._Process == "Store Deposit Receive" & t_StoreMasterData._Incident.In(incidents)).List<t_StoreMasterData>(Storage.Context1).ToList();
                foreach (var dt in data)
                {
                    var task = TaskHelper.GetTask("Store Deposit Receive", dt.Incident.Value, "Finance Confirm");
                    var taskapi = new TaskAPI(task.strTaskId);
                    var temp = new List<string>();
                    temp.Add(taskapi.GetVariableBySingleValue("IsRenovation") == "1" ? "ck" : "");
                    temp.Add("");
                    if (taskapi.GetVariableBySingleValue("IsDevice") == "1")
                    {
                        temp[1] = "ck_" + dt.MachineDeposit;
                    }
                    info.Add(dt.ID, temp);
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                var incidents = AksRequest.GetFormString("Incident").Split(',');
                data = (t_StoreMasterData._Process == "Store Deposit Receive" & t_StoreMasterData._Incident.In(incidents)).List<t_StoreMasterData>(Storage.Context1).ToList();
                foreach (var incidentStr in incidents)
                {
                    // 获流程实例
                    var incident = Convert.ToInt32(incidentStr);
                    var task = TaskHelper.GetTask("Store Deposit Receive", incident, "Finance Confirm");
                    var taskapi = new TaskAPI(task.strTaskId);
                    var storeData = data.FirstOrDefault(c =>c.Incident == Utils.StrToInt(incidentStr,0));
                    // 获取门店
                    var storeId = AksRequest.GetFormInt("StoreID" + incidentStr);
                    var store = (t_smd_Store._StoreID == storeId).ListFirst<t_smd_Store>(Storage.Context3);

                    // 设备保证金
                    if (taskapi.GetVariableBySingleValue("IsDevice") == "1")
                    {
                        var deposit = storeData.MachineDeposit.Value;
                        var SapDeviceNo = AksRequest.GetFormString("SapDeviceNo" + incidentStr);
                        DepositService.SaveDevice(store, true, deposit, SapDeviceNo);
                    }

                    // 履约保证金
                    if (taskapi.GetVariableBySingleValue("IsRenovation") == "1")
                    {
                        var renovationDeposit = AksRequest.GetFormString("RenovationDeposit" + incidentStr);
                        var deposit = Utils.StrToDecimal(renovationDeposit.Replace(",", ""), 0);
                        var SapDocumentNo = AksRequest.GetFormString("SapDocumentNo" + incidentStr);
                        DepositService.SaveRenovation(store, true, deposit, SapDocumentNo);
                    }

                    taskapi.Send();
                }
                ScriptManager.RegisterStartupScript(this, this.GetType(), "info_prompt", "$.jBox.prompt('操作成功！','提示', 'info', { closed: function () { window.opener.reload(); closeWin(); } });", true);
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "info_tip", "$.jBox.tip('" + Resources.Global.C_00_Failed + "');", true);
            }
        }
    }
}